[% setvar title Replace %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Replace =&gt; (stringifying comma) with =&gt; (pair constructor)</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Damian Conway &lt;<a href='mailto:damian@conway.org'>damian@conway.org</a>&gt;
  Date: 10 Aug 2000
  Last Modified: 18 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 84
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>This RFC proposes the introduction of a new data type -- the <i>pair</i> -- and
the co-opting of the =&gt; operator to act as a pair constructor. Most existing
uses of =&gt; would be preserved.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>It is proposed that a new data structure, to be known as a <i>pair</i>, be
added to the language. The =&gt; operator would cease to be a
first-argument-stringifying comma operator, and becomes an anonymous pair
constructor (just as [...] and {...} are anonymous list and hash constructors
respectively). The =&gt; operator would return a reference to a pair containing
the two scalar operands to =&gt;.</p>
<p>The first component of a pair would be called its <i>key</i>, and the
second, it's <i>value</i>. Two new lvalue built-ins -- <code>key</code> and <code>value</code>
-- would be introduced to provide read and write access the components
of a pair.</p>
<pre>	print key $pair_ref;
	value($pair_ref) = $newval;</pre>
<a name='Pairs and hashes'></a><h2>Pairs and hashes</h2>
<p>A hash could be constructed from a list of pair references. In fact a
hash could be thought of (and perhaps implemented!) as a set of pairs.</p>
<p>Thus:</p>
<pre>        %hash = ( a=&gt;1, b=&gt;2, 'c', 3 );</pre>
<p>does what it does in Perl 5, but works slightly differently. The list being
assigned consists of four elements (not six): a pair reference, another pair
reference, a string, and an integer.</p>
<p>When a pair reference is assigned (in)to a hash, the pair's key becomes
the hash entry's key, and the pair's value becomes the entry's value</p>
<p>As the above example indicates, hashes could still be assigned &quot;flat&quot; lists.</p>
<a name='Pairs and arrays'></a><h2>Pairs and arrays</h2>
<p>When a pair reference is assigned (in)to an array, it remains a single scalar
(referential) value. So:</p>
<pre>        @array = ( a=&gt;1, b=&gt;2, 'c', 3 );</pre>
<p>assigns four elements (not six) to @array.</p>
<a name='Pairs and subroutines'></a><h2>Pairs and subroutines</h2>
<p>When a pair reference is used in the argument list of a subroutine with
no parameter list, it is passed as a single scalar value (i.e it remains
a pair reference).</p>
<p>When a pair reference is passed to a subroutine with named parameters, it
binds its value to the parameter of the same name, regardless of the order
in which it is passed.</p>
<p>Thus:</p>
<pre>        use Data::Dumper;

        sub no_params {
                print &quot;no_params:\n&quot;
                print join &quot;\n&quot;, map {ref||&quot;scalar val&quot;} @_;
                print Dumper @_;
        }

        sub params ( $name, $rank, $serial_num) {
                print &quot;params:\n&quot;
                print join &quot;\n&quot;, map {ref||&quot;scalar val&quot;} @_;
                print Dumper @_;
        }

        no_params(serial_num=&gt;1234, name=&gt;'demo', rank=&gt;'RFC');

        params(serial_num=&gt;1234, name=&gt;'demo', rank=&gt;'RFC');</pre>
<p>prints:</p>
<pre>        no_params:
        PAIR
        PAIR
        PAIR
        $VAR1 = ( 'serial_num' =&gt; 1234 );
        $VAR2 = ( 'name' =&gt; 'demo' );
        $VAR3 = ( 'rank' =&gt; 'RFC' );

        params:
        scalar val
        scalar val
        scalar val
        $VAR1 = 'demo';
        $VAR2 = 'RFC';
        $VAR1 = 1234;</pre>
<p>Note that these semantics still support the popular:</p>
<pre>        sub hash_like_args {
                my %args = @_;
                # etc.
        }

        hash_like_args(serial_num=&gt;1234, name=&gt;'demo', rank=&gt;'RFC');</pre>
<a name='Pairs and multiway comparisons'></a><h2>Pairs and multiway comparisons</h2>
<p>Pairs also provide a clean way of implementing multiway comparisons.</p>
<p>It is proposed that when a pair is evaluated in a boolean context, it
would evaluate to the truth value of its key. But when evaluated as the left
operand of a comparison operator, it would evaluate to its value,
<i>but</i> would short-circuit if its key were false.</p>
<p>Thus:</p>
<pre>        1 &lt; 4 &lt; 7 &lt; 9</pre>
<p>would be evaluated:</p>
<pre>        (((1 &lt; 4) &lt; 7) &lt; 9)
        (((true=&gt;4) &lt; 7) &lt; 9)   # true because 1 &lt; 4
        ((true=&gt;7) &lt; 9)         # true because 4 &lt; 7
        (true=&gt;9)               # true because 7 &lt; 9
        1                       # boolean context evals to truth of key</pre>
<p>On the other hand:</p>
<pre>        1 &lt; 4 &lt; 7 &lt; 3 &lt; 9</pre>
<p>would be evaluated:</p>
<pre>        ((((1 &lt; 4) &lt; 7) &lt; 3) &lt; 9)
        ((((true=&gt;4) &lt; 7) &lt; 3) &lt; 9)     # true because 1 &lt; 4
        (((true=&gt;7) &lt; 3) &lt; 9)           # true because 4 &lt; 7
        ((''=&gt;3) &lt; 9)                   # '' (false) because !(7 &lt; 3)
        ''                              # short circuits to false</pre>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>See Description.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>Forthcoming Conway RFC on subroutine parameter lists
(including named parameters)</p>
<p>RFC 25: Operators: Multiway comparisons</p>
</div>
